package com.epam.command.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.command.logic.Command;
import com.epam.command.logic.LoginLogic;
import com.epam.util.ConfigurationManager;

public class RegisterCommand implements Command {

	
	
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {

		String login = request.getParameter("login");
		String password = request.getParameter("password");
		String repeatPassword = request.getParameter("repeatPassword");
		String page;

		if (password.equals(repeatPassword)) {
			if (LoginLogic.register(login, password)) {

				HttpSession session = request.getSession();
				LoginLogic.initializeSession(session, login);
				page = ConfigurationManager.getConfig(ConfigurationManager.OPERATION_SUCCESSFUL);

			} else {

				request.setAttribute("errorMessage", "Login occupied");
				page = ConfigurationManager.getConfig(ConfigurationManager.LOGIN_PAGE);
			}

		} else {
			request.setAttribute("errorMessage", "Password doesn't match");
			page = ConfigurationManager.getConfig(ConfigurationManager.LOGIN_PAGE);

		}

		return page;

	}

}